#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using SqEngine.Components;
using sqengine.Components;
using sqengine.Engine.Common;
using sqengine.PhysicObjects;
#endregion



namespace sqengine.Components
{
    public class ScoreComponent : DrawableGameComponent
    {
        private AdvancedContent _adcontent;
        private bool _enabled;
        private Game _game;

        private PlayerStats _current_player;
        private List<PK3_Model> _botlist;

        private int screen_width = 0;
        private int screen_height = 0;

        private SpriteFont _font;
        private Texture2D _blank;
        private string _font_filename;

        private SpriteBatch _spritebatch;

        public ScoreComponent(Game game, AdvancedContent adcontent, string font)
            : base(game)
        {
            _game = game;
            _adcontent = adcontent;
            _font_filename = font;



        }

        public override void Initialize()
        {
            _spritebatch = new SpriteBatch(_game.GraphicsDevice);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (screen_height == 0 && screen_width == 0)
            {
                screen_width = _game.GraphicsDevice.Viewport.Width;
                screen_height = _game.GraphicsDevice.Viewport.Height / 2 - 100;
            }

            if (_font == null)
            {
                _font = _adcontent.LoadSpriteFont(_font_filename);
                _blank = _adcontent.LoadTexture2D("Textures/UI/blank");
            }
            if (_enabled)
            {
                _botlist = ((sqengine)_game).GetBotList;
                _current_player = ((sqengine)_game).CurrentPlayer;
            }

            base.Update(gameTime);
        }

        protected override void LoadContent()
        {
            _spritebatch = new SpriteBatch(_game.GraphicsDevice);

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {

            if (_enabled)
            {
                _spritebatch.Begin(SpriteBlendMode.AlphaBlend);
                float bot_text_y = _font.MeasureString("A").Y;
                string to_draw = "";
                int posy = screen_height / 2 + 100;
                //_spritebatch.Draw(_blank, new Microsoft.Xna.Framework.Rectangle(screen_width / 2 - 100, posy, 200, 300), Color.Black);
                to_draw = "Score:\n";
                to_draw += "============\n";
                to_draw += string.Format("{0} {1}\n", _current_player.Score, _current_player.Name);
                for (int i = 0; i < _botlist.Count; i++)
                {
                    string bot_text = string.Format("{0} {1}\n", _botlist[i].BotPlayer.Score, _botlist[i].BotPlayer.Name);
                    to_draw += bot_text;
                }

                
        
                   _spritebatch.DrawString(_font, to_draw, new Vector2(screen_width / 2 + 10 - 100, screen_height / 2 + 100), Color.Red);
            
                _spritebatch.End();
            }

            base.Draw(gameTime);

        }

        public void ShowScoreBoard(bool state)
        {
            if (state)
            {
                _enabled = !_enabled;
            }
        }
    }
}